package experiment2.task2;

import experiment2.task1.Person;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author 冯永龙
 * @date 2021/3/23 15:35
 */
class test {
    public static void main(String[] args) {
        PersonIntf personProxy = new PersonProxy(new Person());

        personProxy.setAge(12);
        personProxy.setIdNo("12682348789");
        personProxy.setMerried(true);
        personProxy.setName("张三");

        writeFile((PersonProxy)personProxy);
    }

    public static void writeFile(PersonProxy personProxy) {
        Writer writer = null;
        BufferedWriter bufferedWriter = null;

        try {
            //追加写方式
            String file = "src/experiment2/task2/personProxy.txt";
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true)));
            //取出日志信息写入文件
            ArrayList<String> str = personProxy.getStr();
            Iterator<String> iterator = str.iterator();

            while (iterator.hasNext()) {
                String next = iterator.next();
                bufferedWriter.write(next + "\r\n");
                System.out.println(next);
            }
            bufferedWriter.flush();
        } catch (IOException e) {
            System.out.println("写入文件失败");
            e.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();
                System.out.println("写入文件成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
